#include "music.h"

Music* Music::_instance = 0;

Music* Music::Instance()
{
	if (_instance == 0)
	{
		_instance = new Music();
	}
	return _instance;
}

Music::Music()
{
	string temp;
	_mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32,
		ALLEGRO_CHANNEL_CONF_2);
	_voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16,
		ALLEGRO_CHANNEL_CONF_2);
	al_attach_mixer_to_voice(_mixer, _voice);
	ifstream infile;
	infile.open("musicInformation.txt");
	if (!infile)
	{
		cout << "can't open the music information" << endl;
	}
	while (getline(infile, temp))
	{
		_arrMusic.push_back(al_load_audio_stream(temp.c_str(), 4, 1024));
	}
	_nowPlay = 0;
}

void Music::playMusic()
{
	al_set_audio_stream_playing(_arrMusic[_nowPlay], true);
	al_set_audio_stream_playmode(_arrMusic[_nowPlay], ALLEGRO_PLAYMODE_LOOP);
	al_rewind_audio_stream(_arrMusic[_nowPlay]);
	al_attach_audio_stream_to_mixer(_arrMusic[_nowPlay], _mixer);
}

void Music::stopMusic()
{
	al_set_audio_stream_playing(_arrMusic[_nowPlay], false);
}

void Music::setNowPlayNum(int i)
{
	_nowPlay = i;
}